package com.firebird.app.kotlin.stock.trend.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.firebird.app.kotlin.stock.trend.adapter.WelcomeAdapter
import com.firebird.app.kotlin.stock.trend.R
import java.util.*


/**
 * 初次安装时的引导界面
 * @author ljk
 * @date   2019年10月5日
 */
class WelcomeActivity : Activity(), OnPageChangeListener {
    private var viewPager: ViewPager? = null
    //底部小圆点
    private var dots: Array<ImageView?>?=null
    //当前选中页面
    private var currentIndex = 0

    override fun onCreate(savedInstanceState: Bundle) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_welcome)
        viewPager = findViewById<View>(R.id.viewpager) as ViewPager
        val viewList = pagers
        viewPager!!.adapter = WelcomeAdapter(viewList) //设置viewpager适配器
        //viewPager.setOnPageChangeListener(this);		//设置viewpager切换监听器
        viewPager!!.addOnPageChangeListener(this)
        //初始化底部小圆点
        initDot()
    }//System.out.println("===========>>>> 立即进入Home页面");
    //viewList.add(pager2);
//View pager2 = inflater.inflate(R.layout.item_welcome_b, null);

    //初始化每个导航页面的布局
    @get:SuppressLint("InflateParams")
    private val pagers: List<View>
        private get() {
            val viewList: MutableList<View> = ArrayList()
            val inflater = this.layoutInflater
            val pager1: View = inflater.inflate(R.layout.item_welcome_a, null)
            //View pager2 = inflater.inflate(R.layout.item_welcome_b, null);
            val pager3: View = inflater.inflate(R.layout.item_welcome_c, null)
            val button = pager3.findViewById<View>(R.id.but_comein) as Button
            button.setOnClickListener {
                //System.out.println("===========>>>> 立即进入Home页面");
                val intent = Intent(this@WelcomeActivity, HomeActivity::class.java)
                this@WelcomeActivity.startActivity(intent)
                finish()
            }
            viewList.add(pager1)
            //viewList.add(pager2);
            viewList.add(pager3)
            return viewList
        }

    //初始化底部小圆点
    private fun initDot() {
        val ll = findViewById<View>(R.id.lay_dot) as LinearLayout
        val size = viewPager!!.adapter!!.count
        dots = arrayOfNulls(size)
        for (i in dots!!.indices) {
            dots!![i] = ll.getChildAt(i) as ImageView
            dots!![i]!!.tag = i
            //dots[i].setEnabled(true);
//给下方小圆点绑定点击事件
            dots!![i]!!.setOnClickListener { v ->
                val position = v.tag as Int //获得点击的小圆点位置
                setCurrentDot(position)
                setCurrentView(position)
            }
        }
        dots!![currentIndex]!!.isEnabled = false //初始化时默认选中第一个小圆点
    }

    //设置小圆点被选中状态
    private fun setCurrentDot(position: Int) {
        if (position < 0 || position >= viewPager!!.adapter!!.count || position == currentIndex) {
            return
        }
        dots!![position]!!.isEnabled = false
        dots!![currentIndex]!!.isEnabled = true
        currentIndex = position
    }

    //点击小圆点后切换到响应的页面
    private fun setCurrentView(position: Int) {
        if (position < 0 || position > viewPager!!.adapter!!.count) {
            return
        }
        viewPager!!.currentItem = position
    }

    override fun onPageScrollStateChanged(state: Int) { //state 有三个值：
//SCROLL_STATE_IDLE		滚动完全停止，当前页面填满视图且不再有滚动效果
//SCROLL_STATE_DRAGGING	用户正在拖拽，此时手指与屏幕接触
//SCROLL_STATE_SETTLING 页面开始完成剩下的自动回位过程，此时手指离开屏幕；
//如果手指一直滑动到新的页面填满视图后才松开，则不会出现此状态
//System.out.println("=========>>>>> "+state);
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { //position当前页面
//positionOffset是当前页面滑动比例，如果页面向右翻动，这个值不断变大，最后在趋近1的情况后突变为0。如果页面向左翻动，这个值不断变小，最后变为0。
//positionOffsetPixels是当前页面滑动像素，变化情况和positionOffset一致。
//System.out.println("=========>>>>> "+position +" | "+positionOffset+" | "+positionOffsetPixels);
    }

    //当页面被权重
    override fun onPageSelected(position: Int) {
        setCurrentDot(position)
    }
}














